<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    
    class Admin extends MY_Controller
    {
        // @back_url : used to track previous url 
        var $back_url;
        

        function index($start = FALSE)
        {            
            // Show all companies infos
            $this->load->model('Company_model');
            
            // load pagination
            $this->load->library('pagination');
                        
            
            
            // init config
            $config['base_url'] = site_url('admin/index');
            $config['total_rows'] = $this->Company_model->total_rows();
            $config['per_page'] = $this->page_limit;
            $config['uri_segment'] = 3;

            $this->pagination->initialize($config);

            $this->data['pagination'] = $this->pagination->create_links();
            
            $this->data['companies'] = $this->Company_model->getAllData(FALSE, FALSE, $start, $config['per_page']);                       
            $this->load->view('admin_view',$this->data);                
            // Track back url
            $this->trackBackUrl();            
        }                
        
        function finance()
        {
            redirect('admin/index');
        }
        
        function board()
        {
            redirect('admin/index');
        }
        
        function analyze($type = FALSE)
        {                        
            redirect('admin/index');
        }
        
        function header()
        {       
            $this->data['nav'] = ''; 

            foreach ($this->data['company_info_links'] as $nav) 
            {
                $this->data['nav'] .= '<ul';
                
                if( strstr(substr($_SERVER['PATH_INFO'],1),$nav['link']) )
                {
                    $this->data['nav'] .= ' class="current" ';
                }
                else
                {
                    $this->data['nav'] .= ' class="select" ';
                }

                $this->data['nav'] .= '><li><a href="'.site_url($nav['link']).'"><b>'.$nav['title'].'</b><!--[if IE 7]><!--></a><!--<![endif]-->
                <!--[if lte IE 6]><table><tr><td><![endif]-->
                <div class="select_sub';

                if( strstr(substr($_SERVER['PATH_INFO'],1),$nav['link']) )
                {
                    $this->data['nav'] .= ' show ';
                }               

                $this->data['nav'] .= '"><ul class="sub">';

            foreach ($nav['sub_menu'] as $each) 
            {
                $this->data['nav'] .= '<li ';

                if( strstr(substr($_SERVER['PATH_INFO'],1),$each['link']) )
                {                                                      
                    $this->data['nav'] .= ' class="sub_show" ';                    
                }
                $this->data['nav'] .= '><a href="'.site_url($each['link']).'">'.$each['title'].'</a></li>';
            }    

                $this->data['nav'] .= '</ul>
                </div>
                <!--[if lte IE 6]></td></tr></table></a><![endif]-->
                </li>
                </ul>';
            }

            $this->data['nav'] .= '<div class="nav-divider">&nbsp;</div>';               

            $this->load->view('admin_header',$this->data);
        }
        
        function footer()
        {
            $this->load->view('admin_footer',$this->data);
        }      

        ///////////////////////////////GENERAL FUNCTIONS ///////////////////////////////////////

        // Get news of all companies 
        // @param: new type 
        // @return: list of news which order by descending date (newest to oldest)
        function news($new_type = FALSE, $start = FALSE)
        {           
            if( !$new_type )
            {
                $new_type = 'economic';
            }
            
            $this->data['type'] = $new_type;

            $this->load->model('Company_news_model');

            switch ($new_type) {
                case 'economic':
                    //
                    $this->data['title'] = 'Kinh tế vĩ mô';           

                    // Options 
                    // Economic (type = 1)
                    $options = array('new_types_id' => 1);    

                    $order = array('created_date' => 'desc');   
                    
                    // Enable add new economic articles
                    $this->data['add_link'] = 'admin/addNew/economic';

                    break;
                case 'expert':
                    //
                    $this->data['title'] = 'Nhận định chuyên gia';  

                    // Options 
                    // Expert (type = 2)
                    $options = array('new_types_id' => 2);    
                    $order = array('created_date' => 'desc'); 

                    // Enable add new expert articles
                    $this->data['add_link'] = 'admin/addNew/expert';

                    break;    
                case 'report':
                    //
                    $this->data['title'] = 'Báo cáo phân tích';      

                    // Options 
                    // Report (type = 3)
                    $options = array('new_types_id' => 3);    
                    $order = array('created_date' => 'desc');   
                    
                    // Enable add new report articles
                    $this->data['add_link'] = 'admin/addNew/report';

                    break;    
                case 'market':
                    // 
                    $this->data['title'] = 'Nhận định thị trường';

                    // Options
                    // Market (type = 4)
                    $options = array('new_types_id' => 4);    
                    $order = array('created_date' => 'desc');

                    // Enable add new market articles
                    $this->data['add_link'] = 'admin/addNew/market';
                    
                    break;                
                default:
                    echo 'No defined !';
                    die();
                    break;
            }
            
            // load pagination
            $this->load->library('pagination');
            
            
            // init config
            $config['base_url'] = site_url('admin/news/'.$new_type);
            $config['total_rows'] = $this->Company_news_model->total_rows($options);
            $config['per_page'] = $this->page_limit;
            $config['uri_segment'] = 4;

            $this->pagination->initialize($config);

            $this->data['pagination'] = $this->pagination->create_links();
            
            // Get data
            $this->data['news'] = $this->Company_news_model->getAllData($options, $order, $start, $config['per_page']);
            // Show view
            $this->load->view('news/index',$this->data);
        }

        // Add new type
        // @param: type
        // @return : true if success
        function addNew($new_type = FALSE)
        {                                   
            if( !$new_type )
            {
                redirect('admin/news');
            }

            $this->load->model('Company_news_model');

            // Assign value to type variable
            $this->data['type'] = $new_type;

            $type = 0;

            // Prepare data before adding new !
            switch ($new_type) {
                case 'economic':
                    $this->data['title'] = 'Thêm bài viết Kinh Tế Vĩ Mô';
                    // Expert (type = 1)
                    $type = 1;                     
                    break;                
                case 'expert':                   
                    $this->data['title'] = 'Nhận định chuyên gia mới';
                    // Expert (type = 2)
                    $type = 2;                     
                    break;              
                case 'report':
                    $this->data['title'] = 'Báo cáo phân tích mới';
                    // Expert (type = 3)
                    $type = 3;                     
                    break;                
                case 'market':
                    $this->data['title'] = 'Nhận định thị trường';
                    // Expert (type = 4)
                    $type = 4;                     
                    break;                
                default:
                    echo '<script>javascript:return alert(\'No action defined !\')</script>';
                    die();
                    break;
            }

            // If type still is 0, then exit script !
            if( $type == 0 )
            {
                echo '<script>javascript:return alert(\'Error !!! No type defined !\')</script>';
                die();
            }
            
            
            // If data is submit, then insert record to news 
            if( isset($_POST['submitBtn']) && !empty($_POST['submitBtn']) )
            {            	              
                $this->load->library('form_validation');

                $this->form_validation->set_rules('title','Title','trim|required|xss_clean');
                $this->form_validation->set_rules('detail','Detail','trim|required|xss_clean');

                if( $this->form_validation->run() == FALSE )
                {
                    $this->data['errors'] = form_validation();
					// echo $this->data['errors'];
					// die();
					// Redirect back to add page
                    $this->load->view('news/add',$this->data);
                }
                else
                {                    
                    $new = array(
                            'title'         => $this->input->post('title'),                            
                            'detail'        => $this->input->post('detail'),
                            'created_date'  => date('Y-m-d H:i:s',strtotime('now')),
                            'modified_date' => date('Y-m-d H:i:s',strtotime('now')),
                            'active'        => 1,
                            'slug'          => $this->slug($this->input->post('title')),
                            'new_types_id'  => $type,
                        );                                        
						
					// Upload company report
                    $upload_result = $this->uploadFile('upload');
                    if( $upload_result )
                    {                        
                    	$new['upload'] = $upload_result;
                        $new['filetype'] = $this->getDownloadIcon($new['upload']);
                    }
                    else
                    {
                        /*echo 'Error occurs when processing upload data file';
                        die(); */
                    }	

                    $this->Company_news_model->insert($new);                    
                    redirect('admin/news/'.$new_type,'refresh');
                }
            }
            // Otherwise show add data form
            else
            {            	                              
                $this->load->view('news/add',$this->data);
            }
        }
        
        // Add new type
        // @param: type
        // @return : true if success
        function editNew($new_type = FALSE, $id = FALSE)
        {                                   
            if( !$new_type || !$id )
            {
                redirect('admin/news');
            }                        

            $this->load->model('Company_news_model');
            
            // get new with id
            $new = $this->Company_news_model->getData($id);            
			$editNew = $new;
            
            if( empty($new) )
            {
            	//debug
            	die('oh shit !');
                redirect('admin/news');
            }
            
            $this->data['new_id'] = $id;
            $this->data['new'] = $new;

            // Assign value to type variable
            $this->data['type'] = $new_type;

            $type = 0;

            // Prepare data before adding new !
            switch ($new_type) {
            	case 'economic':
                    $this->data['title'] = 'Sửa bài viết Kinh Tế Vĩ Mô';
                    // Expert (type = 1)
                    $type = 1;                     
                    break;                
                case 'expert':                   
                    $this->data['title'] = 'Sửa Nhận định chuyên gia';
                    // Expert (type = 2)
                    $type = 2;                     
                    break;              
                case 'report':
                    $this->data['title'] = 'Sửa Báo cáo phân tích';
                    // Expert (type = 3)
                    $type = 3;                     
                    break;                
                case 'market':
                    $this->data['title'] = 'Sửa Nhận định thị trường';
                    // Expert (type = 4)
                    $type = 4;                     
                    break;                            
                default:
                    echo '<script>javascript:return alert(\'No action defined !\')</script>';
                    die();
                    break;
            }

            // If type still is 0, then exit script !
            if( $type == 0 )
            {
                echo '<script>javascript:return alert(\'Error !!! No type defined !\')</script>';
                die();
            }
            
            
            // If data is submit, then insert record to news 
            if( isset($_POST['submitBtn']) && !empty($_POST['submitBtn']) )
            {             
                $this->load->library('form_validation');

                $this->form_validation->set_rules('title','Title','trim|required|xss_clean');
                $this->form_validation->set_rules('detail','Detail','trim|required|xss_clean');

                if( $this->form_validation->run() == FALSE )
                {
                    $this->data['errors'] = form_validation();
                    $this->load->view('news/edit',$this->data);
                }
                else
                {
                	// fix bug for "cant use method return value"                	
                	$editTitle = $this->input->post('title');
					$editDetail = $this->input->post('detail');                     
                    $new = array(
                            'title'         => empty($editTitle) ? $new['title'] : $editTitle,
                            'detail'        => empty($editDetail) ? $new['detail'] : $editDetail,
                            'created_date'  => date('Y-m-d H:i:s',strtotime('now')),
                            'modified_date' => date('Y-m-d H:i:s',strtotime('now')),
                            'active'        => 1,
                            'new_types_id'  => $type,
                            'slug'          => empty($editTitle) ? $new['slug'] : $this->slug($editTitle),
                        );     
					
					//debug
					// Upload company report
                    //$upload_result = $this->uploadFile('file');
                    $upload_result = $this->uploadFile('upload');
                    
                    if( isset($_FILES['upload']['name']) )
                    {                                                
                        $new['upload'] = $_FILES['upload']['name'];
                    }
                    else
                    {
                        $new['upload'] = $editNew['upload'];
                    }					
										
                    $new['filetype'] = $this->getDownloadIcon($new['upload']);	                                   

                    if( $this->Company_news_model->update($new, $id) )
                    {
                        redirect('admin/news/'. $new_type);
                    }
                }
            }
            // Otherwise show add data form
            else
            {                
                $this->load->view('news/edit',$this->data);
            }
        }

        function deleteNew($id)
        {
            if( !$id )
            {
                redirect('admin/news');
            }
            
            $this->load->model('Company_news_model');
            
            if( $this->Company_news_model->delete($id) )
            {
                redirect('admin/news');
            }
            else
            {
                redirect('admin/news');
            }
        }

        ////////////////////////////////////////////////////////////////////////////////////////  
        
        // Add new company info here
        function addCompanyInfo()
        {
            $this->load->model('Company_model');
            
            $this->data['company_options_bar'] = $this->showCompanyOptionsBar();
            // If form is submitted, then add data
            // Else show the submit form
            $is_submit = $this->input->post('submitBtn');
            if( $is_submit )
            {
                $this->load->library('form_validation');
                // Validate 
                $this->form_validation->set_rules('company','Company','trim|required|xss_clean');
                $this->form_validation->set_rules('code','Code','trim|required|xss_clean');
                $this->form_validation->set_rules('address','Address','trim|required|xss_clean');
                $this->form_validation->set_rules('website','Website','trim|required|xss_clean');
                $this->form_validation->set_rules('email','Email','trim|required|xss_clean');
                // Run validation
                if( $this->form_validation->run() == FALSE )
                {
                    $this->data['errors'] = validation_errors();
                    $this->data['title'] = 'Tạo công ty mới';                
                    $this->load->view('company/add_company_info',$this->data);
                }
                else
                {
                    $company = array(
                        'name'          =>  $this->input->post('company'),
                        'shortname'     =>  $this->input->post('code'),
                        'address'       =>  $this->input->post('address'),
                        'website'       =>  $this->input->post('website'),
                        'email'         =>  $this->input->post('email'),
                        'slug'          =>  $this->slug($this->input->post('company')),
                        'active'        =>  '1',
                    );
                                        
                    $this->Company_model->insert($company);                    
                    redirect('admin/index');
                }
                
            }
            else
            {
                $this->data['title'] = 'Tạo công ty mới';                
                $this->load->view('company/add_company_info',$this->data);
            }
        }

        // Edit company info
        function editCompanyInfo($id = FALSE)
        {            
            if( !$id )  // If id is empty, then redirect to homepage
            {
                redirect($this->back_url);
            }

            $this->load->model('Company_model');

            $this->data['company_options_bar'] = $this->showCompanyOptionsBar($id);

            // If form is submitted, then edit data
            // Else show the submit form
            $is_submit = $this->input->post('submitBtn');
            if( $is_submit )
            {
                $this->load->library('form_validation');
                // Validate 
                $this->form_validation->set_rules('company','Company','trim|required|xss_clean');
                $this->form_validation->set_rules('code','Code','trim|required|xss_clean');
                $this->form_validation->set_rules('address','Address','trim|required|xss_clean');
                $this->form_validation->set_rules('website','Website','trim|required|xss_clean');
                $this->form_validation->set_rules('email','Email','trim|required|xss_clean');
                $this->form_validation->set_rules('info','Info','trim|required|xss_clean');
                // Run validation
                if( $this->form_validation->run() == FALSE )
                {
                    $this->data['title'] = 'Tạo công ty mới';                
                    $this->load->view('company/add_company_info',$this->data);
                }
                else
                {
                    $company = array(
                        'name'          =>  $this->Company_model->getCurrentData('company'),
                        'shortname'     =>  $this->Company_model->getCurrentData('code'),
                        'address'       =>  $this->Company_model->getCurrentData('address'),
                        'website'       =>  $this->Company_model->getCurrentData('website'),
                        'email'         =>  $this->Company_model->getCurrentData('email'),   
                        'slug'          =>  $this->slug($this->Company_model->getCurrentData('company')),    
                        'info'          =>  $this->Company_model->getCurrentData('info'),   
                        'active'        =>  '1',       
                    );
                    $this->Company_model->update($company, $id);
                    redirect('admin/index');
                }
                
            }
            else
            {
                $this->data['company'] = $this->Company_model->getData($id);                    
                $this->data['title'] = 'Chỉnh sửa thông tin công ty ';                
                $this->load->view('company/edit_company_info',$this->data);
            }
        }
        
        // Edit company finance info
        //
        function editCompanyFinanceInfo($company_id = FALSE, $info_id = FALSE, $start = FALSE)
        {            
            if( !$company_id )  // If id is empty, then redirect to homepage
            {
                redirect($this->back_url);
            }            

            $this->load->model('Company_finance_info_model');

            // Should be check if company id is existed
            $this->data['company_id'] = $company_id;
            $options = array('companies_id' => $company_id);

            // load library pagination
            $this->load->library('pagination');
            
            
            // init config
            if($start === false)
            {
                $start = 0;
            }
            
            $config['base_url'] = site_url('admin/editCompanyFinanceInfo/'.$company_id.'/false/');
            $config['total_rows'] = $this->Company_finance_info_model->total_rows($options);
            $config['per_page'] = $this->page_limit;
            $config['uri_segment'] = 5;

            $this->pagination->initialize($config);

            $this->data['pagination'] = $this->pagination->create_links();
            
            //$this->data['finance'] = $this->Company_finance_info_model->getData(FALSE,$options,$start,$config['per_page']);                          
            $this->data['finances'] = $this->Company_finance_info_model->getAllFinanceInfo($company_id,$start,$config['per_page']);  
            $this->data['company_options_bar'] = $this->showCompanyOptionsBar($company_id);    

            // If you choose edit option, then edit Company Finance option is performed
            // If submit button is pressed, then update Finance option
            // Otherwise, simply show edit company finance info form
            if( $info_id )          
            {   
                //
                $this->data['info_id'] = $info_id;

                //
                $company_finance = $this->Company_finance_info_model->getData($info_id);
                $this->data['company_finance'] = $company_finance;     
                
                if( empty($company_finance) )
                {
                    redirect($this->back_url);
                }

                // If form is submitted, then do updated
                if( isset($_POST['submitBtn']) && !empty($_POST['submitBtn']) )  
                {
                    $company_finace_info = array(
                        'top_balance'           =>  $this->uploadDataV1('top_balance',$company_finance),
                        'balance_increase'      =>  $this->uploadDataV1('balance_increase',$company_finance),
                        'top_contract_profit'   =>  $this->uploadDataV1('top_contract_profit',$company_finance),
                        'top_net_profit'        =>  $this->uploadDataV1('top_net_profit',$company_finance),
                        'net_profit_percent'    =>  $this->uploadDataV1('net_profit_percent',$company_finance),
                        'roe'                   =>  $this->uploadDataV1('roe',$company_finance),
                        'current_money'         =>  $this->uploadDataV1('current_money',$company_finance),
                        'top_asset'             =>  $this->uploadDataV1('top_asset',$company_finance),
                        'vcsh'                  =>  $this->uploadDataV1('vcsh',$company_finance),
                        'total_debt'            =>  $this->uploadDataV1('total_debt',$company_finance),
                        'short_debt'            =>  $this->uploadDataV1('short_debt',$company_finance),
                        'hsn'                   =>  $this->uploadDataV1('hsn',$company_finance),
                        'vnh'                   =>  $this->uploadDataV1('vnh',$company_finance),
                        'hsv'                   =>  $this->uploadDataV1('hsv',$company_finance),
                        'chart'                 =>  $this->uploadDataV1('chart',$company_finance),
                        'year'                  =>  $this->uploadDataV1('year',$company_finance),  
                        'active'                =>  1,
                        );

                    // Update employee
                    $this->Company_finance_info_model->update($company_finace_info, $info_id);
                    redirect('admin/editCompanyFinanceInfo/'.$company_id);
                }
                else
                {
                     $this->data['title'] = 'Thông tin tài chính';                                                
                     $this->load->view('company/edit_company_finance_infoo_detail',$this->data);   
                }
            }
            else // Otherwise show add Company Finance form
            {
                // If form is submitted, then edit data
                // Else show the submit form
                $is_submit = $this->input->post('submitBtn');
                if( $is_submit )
                {
                    $finance = array(
                        'top_balance'           =>  $this->input->post('top_balance'),
                        'balance_increase'      =>  $this->input->post('balance_increase'),
                        'top_contract_profit'   =>  $this->input->post('top_contract_profit'),
                        'top_net_profit'        =>  $this->input->post('top_net_profit'),
                        'net_profit_percent'    =>  $this->input->post('net_profit_percent'),
                        'roe'                   =>  $this->input->post('roe'),
                        'current_money'         =>  $this->input->post('current_money'),
                        'top_asset'             =>  $this->input->post('top_asset'),
                        'vcsh'                  =>  $this->input->post('vcsh'),
                        'total_debt'            =>  $this->input->post('total_debt'),
                        'short_debt'            =>  $this->input->post('short_debt'),
                        'hsn'                   =>  $this->input->post('hsn'),
                        'vnh'                   =>  $this->input->post('vnh'),
                        'hsv'                   =>  $this->input->post('hsv'),
                        'year'                  =>  $this->input->post('year'),
                        'companies_id'          =>  $company_id,
                        'active'                =>  1,
                    );
                   
                    $upload_result = $this->uploadFile('upload');
                    
                    if( isset($_FILES['upload']['name']) && $upload_result)
                    {                                                
                        $finance['chart'] = $_FILES['upload']['name'];
                    }
                   
                    
                    $this->Company_finance_info_model->insert($finance);                        
                    redirect('admin/editCompanyFinanceInfo/'.$company_id);
                }
                else
                {                                    
                    $this->data['title'] = 'Thông tin tài chính công ty ';                
                    $this->load->view('company/edit_company_finance_infoo',$this->data);
                }
            }
        }   

        // Edit company finance 
        function editCompanyFinance($company_id = FALSE, $edit_id = FALSE, $start = FALSE)
        {            
            if( !$company_id )  // If id is empty, then redirect to homepage
            {
                redirect($this->back_url);
            }            

            $this->load->model('Company_finance_model');

            // Should be check if company id is existed
            $this->data['company_id'] = $company_id;
            $options = array('company_id' => $company_id);
            
            // load library pagination
            $this->load->library('pagination');
            
            
            // init config
            $config['base_url'] = site_url('admin/editCompanyFinance/'.$company_id.'/false/');
            $config['total_rows'] = $this->Company_finance_model->total_rows($options);
            $config['per_page'] = $this->page_limit;
            $config['uri_segment'] = 5;

            $this->pagination->initialize($config);

            $this->data['pagination'] = $this->pagination->create_links();
                       
            $this->data['finance'] = $this->Company_finance_model->getData(FALSE,$options,$start,$config['per_page']);                          

            $this->data['company_options_bar'] = $this->showCompanyOptionsBar($company_id);    

            // If you choose edit option, then edit Company Finance option is performed
            // If submit button is pressed, then update Finance option
            // Otherwise, simply show edit company finance info form
            if( $edit_id )          
            {
                //
                $this->data['edit_id'] = $edit_id;

                //
                $company_finance = $this->Company_finance_model->getData($edit_id);
                $this->data['company_finance'] = $company_finance;

                if( empty($company_finance) )
                {
                    redirect($this->back_url);
                }

                // If form is submitted, then do updated
                if( isset($_POST['submitBtn']) && !empty($_POST['submitBtn']) )  
                {
                    $this->load->library('form_validation');

                    // Validate 
                    $this->form_validation->set_rules('vdl','VDL','trim|numeric|xss_clean');
                    $this->form_validation->set_rules('eps','EPS','trim|numeric|xss_clean');
                    $this->form_validation->set_rules('dps','DPS','trim|xss_clean');
                    $this->form_validation->set_rules('bv','BV','trim|numeric|xss_clean');
                    $this->form_validation->set_rules('roe','ROE','trim|numeric|xss_clean');
                    $this->form_validation->set_rules('dtratio','DT Ratio','trim|numeric|xss_clean');
                    $this->form_validation->set_rules('lnratio','LN Ratio','trim|numeric|xss_clean');
                    $this->form_validation->set_rules('debt','Debt','trim|xss_clean');
                    $this->form_validation->set_rules('price','Price','trim|xss_clean');
                    $this->form_validation->set_rules('suggest','Suggest','trim|xss_clean');

                    // Run validation
                    if( $this->form_validation->run() == FALSE )
                    {
                        $this->data['errors'] = validation_errors();
                        $this->data['title'] = 'Bảng giá cổ phiếu';                
                        $this->load->view('company/edit_company_finance_info_detail',$this->data);
                    }
                    else
                    {
                        $company_finace_info = array(
                            'vdl'         =>  $this->uploadDataV1('vdl',$company_finance),
                            'eps'         =>  $this->uploadDataV1('eps',$company_finance),
                            'dps'         =>  $this->uploadDataV1('dps',$company_finance),
                            'bv'          =>  $this->uploadDataV1('bv',$company_finance),
                            'roe'         =>  $this->uploadDataV1('roe',$company_finance),   
                            'dtratio'     =>  $this->uploadDataV1('dtratio',$company_finance),
                            'lnratio'     =>  $this->uploadDataV1('lnratio',$company_finance),       
                            'debt'        =>  $this->uploadDataV1('debt',$company_finance),
                            'price'       =>  $this->uploadDataV1('price',$company_finance),
                            'suggest'     =>  $this->uploadDataV1('suggest',$company_finance),                            
                            'active'      =>  1,
                            );

                        // Update employee
                        $this->Company_finance_model->update($company_finace_info, $edit_id);
                        redirect('admin/editCompanyFinance/'.$company_id);
                    }
                }
                else
                {
                     $this->data['title'] = 'Bảng giá cổ phiếu';                                                
                     $this->load->view('company/edit_company_finance_info_detail',$this->data);   
                }
            }
            else // Otherwise show add Company Finance form
            {
                // If form is submitted, then edit data
                // Else show the submit form
                $is_submit = $this->input->post('submitBtn');
                if( $is_submit )
                {
                    $this->load->library('form_validation');
                    // Validate 
                    $this->form_validation->set_rules('vdl','VDL','trim|required|numeric|xss_clean');
                    $this->form_validation->set_rules('eps','EPS','trim|required|numeric|xss_clean');
                    $this->form_validation->set_rules('dps','DPS','trim|required|xss_clean');
                    $this->form_validation->set_rules('bv','BV','trim|required|numeric|xss_clean');
                    $this->form_validation->set_rules('roe','ROE','trim|required|numeric|xss_clean');
                    $this->form_validation->set_rules('dtratio','DT Ratio','trim|required|numeric|xss_clean');
                    $this->form_validation->set_rules('lnratio','LN Ratio','trim|required|numeric|xss_clean');
                    $this->form_validation->set_rules('debt','Debt','trim|required|xss_clean');
                    $this->form_validation->set_rules('price','Price','trim|required|xss_clean');
                    $this->form_validation->set_rules('suggest','Suggest','trim|required|xss_clean');

                    // Run validation
                    if( $this->form_validation->run() == FALSE )
                    {
                        $this->data['errors'] = form_validation();
                        $this->data['title'] = 'Bảng giá cổ phiếu';                
                        $this->load->view('company/edit_company_finance_info',$this->data);
                    }
                    else
                    {
                        $finance = array(
                            'vdl'         =>  $this->input->post('vdl'),
                            'eps'         =>  $this->input->post('eps'),
                            'dps'         =>  $this->input->post('dps'),
                            'bv'          =>  $this->input->post('bv'),
                            'roe'         =>  $this->input->post('roe'),   
                            'dtratio'     =>  $this->input->post('dtratio'),
                            'lnratio'     =>  $this->input->post('lnratio'),       
                            'debt'        =>  $this->input->post('debt'),
                            'price'       =>  $this->input->post('price'),
                            'suggest'     =>  $this->input->post('suggest'),
                            'company_id'  =>  $company_id,
                            'active'      =>  1,
                        );
                        $this->Company_finance_model->insert($finance);
                        redirect('admin/editCompanyFinance/'.$company_id);
                    }
                    
                }
                else
                {                                    
                    $this->data['title'] = 'Bảng giá cổ phiếu';                
                    $this->load->view('company/edit_company_finance_info',$this->data);
                }
            }
            
        }        

        // Delete company finance
        //
        //
        function deleteCompanyFinance($company_id = FALSE, $edit_id = FALSE)
        {
            $this->load->model('Company_finance_model');

            // If employee id is present, then do:
            // get employee data. If employee is available, then do delete (active = 0)
            // after delete, back to editCompanyBoard 
            if( $edit_id )            
            {                
                $finance = $this->Company_finance_model->getData($edit_id);
                if( empty($finance) )
                {
                    redirect('admin/editCompanyFinance/'.$company_id);
                }
                else
                {
                    // Delete company finance (active = 0)
                    if( $this->Company_finance_model->delete($edit_id) )
                    {
                        redirect('admin/editCompanyFinance/'.$company_id);
                    }                                        
                }
            }
            else // Otherwise it maybe delete finance (Not available at this time)
            {
                // It should be back to editCompanyBoard
                if( $company_id )
                {                    
                    $company_finance = $this->Company_finance_model->getData(FALSE,array('company_id',$company_id));

                    if( empty($company_finance) )
                    {
                        redirect($this->back_url);
                    }
                    else
                    {
                        redirect('admin/editCompanyFinance/'.$company_id);
                    }
                }
                else
                {
                    // Empty company_id ! Redirect to back url
                    redirect($this->back_url);
                }
            }
        }

        // Edit company boards 
        // @param: $company_id 
        // @return: if submit is not processed, then table of datas will show
        // otherwise 
        function editCompanyBoard($company_id = FALSE, $employee_id = FALSE, $start = FALSE)
        {            
            $this->load->model('Employee_model');

            // If company id is empty, then do nothing
            if( empty($company_id) )
            {
                redirect($this->back_url);
            }

			// Load model position
			$this->load->model('Position_model');
			$positions = $this->Position_model->getAllData();
            
			$this->data['positions'] = $positions;
			
            // Should be check if company id is existed
            $this->data['company_id'] = $company_id;
            $options = array('companies_id' => $company_id);
            
            // load library pagination
            $this->load->library('pagination');
            
            
            // init config
            $config['base_url'] = site_url('admin/editCompanyBoard/'.$company_id.'/false/');
            $config['total_rows'] = $this->Employee_model->total_rows($options);
            $config['per_page'] = $this->page_limit;
            $config['uri_segment'] = 5;

            $this->pagination->initialize($config);

            $this->data['pagination'] = $this->pagination->create_links();

            $this->data['company_options_bar'] = $this->showCompanyOptionsBar($company_id);       
            

            
            $this->data['employees'] = $this->Employee_model->getAllData($options, FALSE, $start, $config['per_page']);  

           // If employee id is specified, then show edit employee form
            if( $employee_id )
            {
                $employee = $this->Employee_model->getData($employee_id);

                if( empty($employee) )
                {
                    redirect($this->back_url);
                }
                else
                {
                    $this->data['employee_id'] = $employee_id;
                    $this->data['employee'] = $employee;

                    // If form is submitted, then do updated employee data 
                    if( isset($_POST['submitBtn']) && !empty($_POST['submitBtn']) )
                    {
                        $this->load->library('form_validation');

                        // Validate 
                        $this->form_validation->set_rules('fullname','FullName','trim|xss_clean');
                        $this->form_validation->set_rules('position','Position','trim|xss_clean');
                        $this->form_validation->set_rules('age','Age','trim|numeric|xss_clean');
                        $this->form_validation->set_rules('work_process','WorkProcess','trim|xss_clean');                
                        // Run validation
                        if( $this->form_validation->run() == FALSE )
                        {
                            $this->data['errors'] = validation_errors();
                            $this->data['title'] = 'Ban quản trị công ty';                
                            $this->load->view('company/edit_company_employee',$this->data);
                        }
                        else
                        {
                            $employee = array(
                                'fullname'          =>  $this->uploadDataV1('fullname',$employee),
                                'image'             =>  $this->uploadDataV2('image',$employee),
                                'position'          =>  $this->uploadDataV1('position',$employee),
                                'age'               =>  $this->uploadDataV1('age',$employee),
                                'work_process'      =>  $this->uploadDataV1('work_process',$employee),                               
                                'active'            =>  1,
                            );

                            // Update employee
                            $this->Employee_model->update($employee, $employee_id);
                            redirect('admin/editCompanyBoard/'.$company_id);
                        }
                    }
                    else // Show edit employee data
                    {
                        $this->data['title'] = 'Thông tin thành viên';                                                
                        $this->load->view('company/edit_company_employee',$this->data);
                    }
                }
            }   // Otherwise show add employee form
            else
            {
                if( !$company_id )  // If id is empty, then redirect to homepage
                {
                    redirect($this->back_url);
                }                                                 

                // If form is submitted, then edit data
                // Else show the submit form
                $is_submit = $this->input->post('submitBtn');
                if( $is_submit )
                {
                    $this->load->library('form_validation');
                    // Validate 
                    $this->form_validation->set_rules('fullname','FullName','trim|required|xss_clean');
                    $this->form_validation->set_rules('position','Position','trim|required|xss_clean');
                    $this->form_validation->set_rules('age','Age','trim|numeric|xss_clean');
                    $this->form_validation->set_rules('work_process','WorkProcess','trim|xss_clean');                
                    // Run validation
                    if( $this->form_validation->run() == FALSE )
                    {
                        $this->data['errors'] = validation_errors();
                        $this->data['title'] = 'Ban quản trị công ty';                
                        $this->load->view('company/edit_company_board',$this->data);
                    }
                    else
                    {
                        $employee = array(
                            'fullname'          =>  $this->input->post('fullname'),
                            'position'          =>  $this->input->post('position'),
                            'age'               =>  $this->input->post('age'),
                            'work_process'      =>  $this->input->post('work_process'),   
                            'companies_id'      =>  $company_id,
                            'active'            =>  1,
                        );        

                        // Upload employee image
                        $upload_result = $this->uploadImage();
                        if( !isset($upload_result['error']) && empty($upload_result['error']) )
                        {                        
                            $employee['image'] = $upload_result['upload_data']['file_name'];
                        }
                        else
                        {
                            print_r($upload_result);
                            die();
                        }

                        $this->Employee_model->insert($employee);
                        redirect('admin/editCompanyBoard/'.$company_id);
                    }                
                }
                else
                {                                      
                    $this->data['title'] = 'Ban quản trị công ty ';                                                
                    $this->load->view('company/edit_company_board',$this->data);
                }
            }
            
        }

        // Edit company report 
        // @param: $company_id 
        // @return: if submit is not processed, then table of datas will show
        // otherwise 
        function editCompanyAnalyzeBasic($company_id = FALSE, $report_id = FALSE, $start = FALSE)
        {            
            $this->load->model('Report_model');

            // If company id is empty, then do nothing
            if( empty($company_id) )
            {
                redirect($this->back_url);
            }

            // Should be check if company id is existed
            $this->data['company_id'] = $company_id;
            $options = array('companies_id' => $company_id);
            
            // load library pagination
            $this->load->library('pagination');
            
            
            // init config
            $config['base_url'] = site_url('admin/editCompanyAnalyzeBasic/'.$company_id.'/false/');
            $config['total_rows'] = $this->Report_model->total_rows($options);
            $config['per_page'] = $this->page_limit;
            $config['uri_segment'] = 5;

            $this->pagination->initialize($config);

            $this->data['pagination'] = $this->pagination->create_links();

            $this->data['company_options_bar'] = $this->showCompanyOptionsBar($company_id);       
           
            $this->data['reports'] = $this->Report_model->getAllData($options,FALSE,$start,$config['per_page']);  

           // If report id is specified, then show edit report form
            if( $report_id )
            {
                $report = $this->Report_model->getData($report_id);

                if( empty($report) )
                {
                    redirect($this->back_url);
                }
                else
                {
                    $this->data['report_id'] = $report_id;
                    $this->data['report'] = $report;

                    // If form is submitted, then do updated employee data 
                    if( isset($_POST['submitBtn']) && !empty($_POST['submitBtn']) )
                    {
                        $this->load->library('form_validation');

                        // Validate 
                        $this->form_validation->set_rules('title','Title','trim|xss_clean');
                        $this->form_validation->set_rules('upload','Upload','trim|xss_clean');
                                       
                        // Run validation
                        if( $this->form_validation->run() == FALSE )
                        {
                            $this->data['errors'] = validation_errors();
                            $this->data['title'] = 'Phân tích cơ bản';                
                            $this->load->view('company/edit_company_analyze_basic_detail',$this->data);
                        }
                        else
                        {
                            $report = array(
                                'title'          =>  $this->uploadDataV1('title',$report),
                                'upload'         =>  $this->uploadDataV2('upload',$report),                                
                                'active'         =>  1,
                            );

                            $report['filetype'] = $this->getDownloadIcon($report['upload']);
                            // Update report
                            $this->Report_model->update($report, $report_id);
                            redirect('admin/editCompanyAnalyzeBasic/'.$company_id);
                        }
                    }
                    else // Show edit employee data
                    {
                        $this->data['title'] = 'Phân tích cơ bản';                                                
                        $this->load->view('company/edit_company_analyze_basic_detail',$this->data);
                    }
                }
            }   // Otherwise show add employee form
            else
            {
                if( !$company_id )  // If id is empty, then redirect to homepage
                {
                    redirect($this->back_url);
                }                                                 

                // If form is submitted, then edit data
                // Else show the submit form
                $is_submit = $this->input->post('submitBtn');
                if( $is_submit )
                {
                    $this->load->library('form_validation');
                    // Validate 
                    $this->form_validation->set_rules('title','Title','trim|required|xss_clean');
                    $this->form_validation->set_rules('upload','Upload','trim|xss_clean');
                    
                    // Run validation
                    if( $this->form_validation->run() == FALSE )
                    {
                        $this->data['errors'] = validation_errors();
                        $this->data['title'] = 'Phân tích cơ bản';                
                        $this->load->view('company/edit_company_analyze_basic',$this->data);
                    }
                    else
                    {
                        $report = array(
                            'title'          =>  $this->input->post('title'),
                            'active'         =>  1,                            
                            'created_date'   =>  strtotime('Y-m-d H:i:s',date('now')),
                            'companies_id'   =>  $company_id,
                        );

                        // Upload company report
                        $upload_result = $this->uploadFile('upload');
                        if( $upload_result )
                        {                        
                            $report['upload'] = $upload_result;
                            $report['filetype'] = $this->getDownloadIcon($report['upload']);
                        }
                        else
                        {
                            echo 'Error occurs when processing upload data file';
                            die();
                        }

                        $this->Report_model->insert($report);
                        redirect('admin/editCompanyAnalyzeBasic/'.$company_id);
                    }                
                }
                else
                {                                      
                    $this->data['title'] = 'Phân tích cơ bản';                                                
                    $this->load->view('company/edit_company_analyze_basic',$this->data);
                }
            }
            
        }

		// Edit company report 
        // @param: $company_id 
        // @return: if submit is not processed, then table of datas will show
        // otherwise 
        function editCompanyAnalyzeTechnical($company_id = FALSE, $report_id = FALSE, $start = FALSE)
        {            
            $this->load->model('Report_model');

            // If company id is empty, then do nothing
            if( empty($company_id) )
            {
                redirect($this->back_url);
            }

            // Should be check if company id is existed
            $this->data['company_id'] = $company_id;
            $options = array('companies_id' => $company_id,'report_types_id'=>1);
            
            // load library pagination
            $this->load->library('pagination');
            
            
            // init config
            $config['base_url'] = site_url('admin/editCompanyAnalyzeTechnical/'.$company_id.'/false/');
            $config['total_rows'] = $this->Report_model->total_rows($options);
            $config['per_page'] = $this->page_limit;
            $config['uri_segment'] = 5;

            $this->pagination->initialize($config);

            $this->data['pagination'] = $this->pagination->create_links();

            $this->data['company_options_bar'] = $this->showCompanyOptionsBar($company_id);       
           
            $this->data['reports'] = $this->Report_model->getAllData($options,FALSE,$start,$config['per_page']);  

           // If report id is specified, then show edit report form
            if( $report_id )
            {
                $report = $this->Report_model->getData($report_id);

                if( empty($report) )
                {
                    redirect($this->back_url);
                }
                else
                {
                    $this->data['report_id'] = $report_id;
                    $this->data['report'] = $report;

                    // If form is submitted, then do updated employee data 
                    if( isset($_POST['submitBtn']) && !empty($_POST['submitBtn']) )
                    {
                        $this->load->library('form_validation');

                        // Validate 
                        $this->form_validation->set_rules('title','Title','trim|xss_clean');
                        $this->form_validation->set_rules('upload','Upload','trim|xss_clean');
                                       
                        // Run validation
                        if( $this->form_validation->run() == FALSE )
                        {
                            $this->data['errors'] = validation_errors();
                            $this->data['title'] = 'Phân tích kỹ thuật';                
                            $this->load->view('company/edit_company_analyze_technical_detail',$this->data);
                        }
                        else
                        {
                            $report = array(
                                'title'          =>  $this->uploadDataV1('title',$report),
                                'upload'         =>  $this->uploadDataV2('upload',$report),
                                'report_types_id'	=>	1,                                
                                'active'         =>  1,
                            );

                            $report['filetype'] = $this->getDownloadIcon($report['upload']);
                            // Update report
                            $this->Report_model->update($report, $report_id);
                            redirect('admin/editCompanyAnalyzeTechnical/'.$company_id);
                        }
                    }
                    else // Show edit employee data
                    {
                        $this->data['title'] = 'Phân tích cơ bản';                                                
                        $this->load->view('company/edit_company_analyze_technical_detail',$this->data);
                    }
                }
            }   // Otherwise show add employee form
            else
            {
                if( !$company_id )  // If id is empty, then redirect to homepage
                {
                    redirect($this->back_url);
                }                                                 

                // If form is submitted, then edit data
                // Else show the submit form
                $is_submit = $this->input->post('submitBtn');
                if( $is_submit )
                {
                    $this->load->library('form_validation');
                    // Validate 
                    $this->form_validation->set_rules('title','Title','trim|required|xss_clean');
                    $this->form_validation->set_rules('upload','Upload','trim|xss_clean');
                    
                    // Run validation
                    if( $this->form_validation->run() == FALSE )
                    {
                        $this->data['errors'] = validation_errors();
                        $this->data['title'] = 'Phân tích kỹ thuật';                
                        $this->load->view('company/edit_company_analyze_technical',$this->data);
                    }
                    else
                    {
                        $report = array(
                            'title'          =>  $this->input->post('title'),
                            'active'         =>  1,                            
                            'created_date'   =>  strtotime('Y-m-d H:i:s',date('now')),
                            'companies_id'   =>  $company_id,
                            'report_types_id'	=>	1,
                        );

                        // Upload company report
                        $upload_result = $this->uploadFile('upload');
                        if( $upload_result )
                        {                        
                            $report['upload'] = $upload_result;
                            $report['filetype'] = $this->getDownloadIcon($report['upload']);
                        }
                        else
                        {
                            echo 'Error occurs when processing upload data file';
                            die();
                        }

                        $this->Report_model->insert($report);
                        redirect('admin/editCompanyAnalyzeTechnical/'.$company_id);
                    }                
                }
                else
                {                                      
                    $this->data['title'] = 'Phân tích cơ bản';                                                
                    $this->load->view('company/edit_company_analyze_technical',$this->data);
                }
            }
            
        }
		
        // Delete company report
        //
        //
        function deleteCompanyAnalyzeBasic($company_id = FALSE, $report_id = FALSE)
        {
            $this->load->model('Report_model');

            // If report id is present, then do:
            // get report data. If report is available, then do delete (active = 0)
            // after delete, back to editCompanyAnalyzeBasic 
            if( $report_id )            
            {                
                $report = $this->Report_model->getData($report_id);
                if( empty($report) )
                {
                    redirect('admin/editCompanyAnalyzeBasic/'.$company_id);
                }
                else
                {
                    // Delete report (active = 0)
                    if( $this->Report_model->delete($report_id) )
                    {
                        redirect('admin/editCompanyAnalyzeBasic/'.$company_id);
                    }                                        
                }
            }
            else // Otherwise it maybe delete company (Not available at this time)
            {
                // It should be back to editCompanyBoard
                if( $company_id )
                {                    
                    $reports = $this->Report_model->getData(FALSE,array('companies_id',$company_id));

                    if( empty($reports) )
                    {
                        redirect($this->back_url);
                    }
                    else
                    {
                        redirect('admin/editCompanyAnalyzeBasic/'.$company_id);
                    }
                }
                else
                {
                    // Empty company_id ! Redirect to back url
                    redirect($this->back_url);
                }
            }
        }

		// Delete company report
        //
        //
        function deleteCompanyAnalyzeTechnical($company_id = FALSE, $report_id = FALSE)
        {
            $this->load->model('Report_model');

            // If report id is present, then do:
            // get report data. If report is available, then do delete (active = 0)
            // after delete, back to editCompanyAnalyzeBasic 
            if( $report_id )            
            {                
                $report = $this->Report_model->getData($report_id);
                if( empty($report) )
                {
                    redirect('admin/editCompanyAnalyzeTechnical/'.$company_id);
                }
                else
                {
                    // Delete report (active = 0)
                    if( $this->Report_model->delete($report_id) )
                    {
                        redirect('admin/editCompanyAnalyzeTechnical/'.$company_id);
                    }                                        
                }
            }
            else // Otherwise it maybe delete company (Not available at this time)
            {
                // It should be back to editCompanyBoard
                if( $company_id )
                {                    
                    $reports = $this->Report_model->getData(FALSE,array('companies_id' => $company_id,'report_types_id' => 1));

                    if( empty($reports) )
                    {
                        redirect($this->back_url);
                    }
                    else
                    {
                        redirect('admin/editCompanyAnalyzeTechnical/'.$company_id);
                    }
                }
                else
                {
                    // Empty company_id ! Redirect to back url
                    redirect($this->back_url);
                }
            }
        }

        // Delete company employee
        //
        //
        function deleteCompanyBoard($company_id = FALSE, $employee_id = FALSE)
        {
            $this->load->model('Employee_model');

            // If employee id is present, then do:
            // get employee data. If employee is available, then do delete (active = 0)
            // after delete, back to editCompanyBoard 
            if( $employee_id )            
            {                
                $employee = $this->Employee_model->getData($employee_id);
                if( empty($employee) )
                {
                    redirect('admin/editCompanyBoard/'.$company_id);
                }
                else
                {
                    // Delete employee (active = 0)
                    if( $this->Employee_model->delete($employee_id) )
                    {
                        redirect('admin/editCompanyBoard/'.$company_id);
                    }                                        
                }
            }
            else // Otherwise it maybe delete board (Not available at this time)
            {
                // It should be back to editCompanyBoard
                if( $company_id )
                {                    
                    $employee = $this->Employee_model->getData(FALSE,array('companies_id',$company_id));

                    if( empty($employee) )
                    {
                        redirect($this->back_url);
                    }
                    else
                    {
                        redirect('admin/editCompanyBoard/'.$company_id);
                    }
                }
                else
                {
                    // Empty company_id ! Redirect to back url
                    redirect($this->back_url);
                }
            }
        }

        // Delete company info
        function deleteCompanyInfo($id = FALSE)
        {                    
            if( $id )
            {
                $this->load->model('Company_model');

                if ( $this->Company_model->delete($id) )
                {                    
                    $this->data['message'] = $this->showSuccess('Delete success !');                    
                }
                else
                {
                    $this->data['message'] = $this->showError('Pls try again !');
                }
            }
            else
            {                
                $this->data['message'] = $this->showWarning('No action support !');
            }

            redirect('admin/index',$this->data);
        }
        
        /*
         * Nhà đầu tư
         */
        function invest()
        {
            // Đầu tư nước ngoài và trong nước
            redirect('admin/investor/ndt_trong_nuoc');
        }
        
        /*
         * 
         */
        function investor($type = false,$action = false, $id = FALSE)
        {            
           
            //                          type = 1           type = 2
            $investor_types = array('ndt_trong_nuoc','ndt_nuoc_ngoai');
            
            if(!$type && !in_array($type, $investor_types) )
            {
                $type = 'ndt_trong_nuoc';
            }
            
            $this->load->model('Company_investor_model');
            
            $options = array();
            switch ($type)
            {
                case 'ndt_trong_nuoc':
                    $options = array('investors_id' => 1);
                    break;
                case 'ndt_nuoc_ngoai':
                    $options = array('investors_id' => 2);
                    break;
                default:
                    break;
            }
            
            // edit case
            if( $action == 'edit' )
            {
                if( $id )
                {
                    $investor = $this->Company_investor_model->getData($id);
                    $this->data['investor'] = $investor;
                    
                    if( !empty($investor) )
                    {
                        $submitBtn = $this->input->post('submitBtn');
                        if( isset($submitBtn) && !empty($submitBtn) )
                        {
                            $this->load->library('form_validation');
                            // validation
                            $this->form_validation->set_rules('name','Name','trim|xss_clean');
                            $this->form_validation->set_rules('sell','Name','trim|xss_clean');
                            $this->form_validation->set_rules('buy','Name','trim|xss_clean');
                            
                            if( $this->form_validation->run() == FALSE )
                            {
                                $this->data['errors'] = validation_errors();                                
                            }
                            else
                            {
                                $type_input = $this->input->post('type');
                                
                                if( $type_input == 'ndt_trong_nuoc' )
                                {
                                    $type_input = 1;
                                }
                                else if( $type_input == 'ndt_nuoc_ngoai' )
                                {
                                    $type_input = 2;
                                }
                                                                
                                $investor_input = array(
                                    'name'  =>  $this->Company_investor_model->getCurrentData('name', $investor),
                                    'sell'  =>  $this->Company_investor_model->getCurrentData('sell', $investor),
                                    'buy'  =>  $this->Company_investor_model->getCurrentData('buy', $investor),
                                    'investors_id'  =>  $type_input,
                                );
                                
                                $this->Company_investor_model->update($investor_input, $id);    
                                redirect('admin/investor/'.$type, 'refresh');
                            }                                                                                    
                        }                        
                    }
                }
            }
            
            $this->data['investors_data'] = $this->Company_investor_model->getAllData($options);
            
            // in case data is empty then create fake data
            if( empty($this->data['investors_data']) )
            {
                if( $type == 'ndt_trong_nuoc' )
                {
                    $type = 1;
                }
                else if( $type == 'ndt_nuoc_ngoai' )
                {
                    $type = 2;
                }
                
                
                $this->data = array(
                    'name'          => 'KL',
                    'sell'           =>  '0.0',
                    'buy'           =>  '0.0',
                    'active'        =>   1,
                    'investors_id'   =>  $type,
                );
                        
                // insert KL and GT
                $i = 1;
                while( $i < 3)
                {
                    if( $i == 2 )
                    {
                        $this->data['name'] = 'GT';
                    }                    
                    
                    $this->Company_investor_model->insert($this->data);
                    $i++;
                }
                
                
                $this->data['investors_data'] = $this->Company_investor_model->getAllData($options);
            }
            
            if( $type == 'ndt_trong_nuoc' )
            {
                $this->data['title'] = 'Nhà đầu tư trong nước';
            }
            else if( $type == 'ndt_nuoc_ngoai' )
            {
                $this->data['title'] = 'Nhà đầu tư nước ngoài';
            }
            
            //type
            $this->data['type'] = $type;
            
            // options 
            $this->data['options_bar'] = $this->showInvestorOptionsBar();
            
            $this->load->view('investor/index', $this->data);
                        
        }
                
        
        // void function
        function trackBackUrl()
        {
            if( $this->session->userdata('back_url') )
            {
                $this->back_url = $this->session->userdata('back_url');
            }
            else
            {
                $this->back_url = 'admin/index';
            }
        }
		
		// Index position
		function positions($start = FALSE)
        {            
            // Show all positions infos
            $this->load->model('Position_model');
            
            // load pagination
            $this->load->library('pagination');
                                                
            // init config
            $config['base_url'] = site_url('admin/positions');
            $config['total_rows'] = $this->Position_model->total_rows();
            $config['per_page'] = $this->page_limit;
            $config['uri_segment'] = 3;

            $this->pagination->initialize($config);

            $this->data['pagination'] = $this->pagination->create_links();
            
            $this->data['positions'] = $this->Position_model->getAllData(FALSE, FALSE, $start, $config['per_page']);                       
            $this->load->view('positions/index',$this->data);                
            // Track back url
            $this->trackBackUrl();            
        }          
		
		//
		// Add new position info here
        function addPositionInfo()
        {
            $this->load->model('Position_model');
			            
            $this->data['company_options_bar'] = $this->showCompanyOptionsBar();
            // If form is submitted, then add data
            // Else show the submit form
            $is_submit = $this->input->post('submitBtn');
            if( $is_submit )
            {
                $this->load->library('form_validation');
                // Validate 
                $this->form_validation->set_rules('position','Position','trim|required|xss_clean');                
                // Run validation
                if( $this->form_validation->run() == FALSE )
                {
                    $this->data['errors'] = validation_errors();
                    $this->data['title'] = 'Tạo chức vụ mới';                
                    $this->load->view('positions/add_position_info',$this->data);
                }
                else
                {
                    $position = array(
                        'position'          =>  $this->input->post('position'),  
                        'active'	=> 1,                      
                    );
                                        
                    $this->Position_model->insert($position);                    
                    redirect('admin/positions');
                }
                
            }
            else
            {
                $this->data['title'] = 'Tạo chức vụ mới';                
                $this->load->view('positions/add_position_info',$this->data);
            }
        }

        // Edit company info
        function editPositionInfo($id = FALSE)
        {            
            if( !$id )  // If id is empty, then redirect to homepage
            {
                redirect($this->back_url);
            }

            $this->load->model('Position_model');

            $this->data['company_options_bar'] = $this->showCompanyOptionsBar($id);
			
			$this->data['position'] = $this->Position_model->getData($id);   

            // If form is submitted, then edit data
            // Else show the submit form
            $is_submit = $this->input->post('submitBtn');
            if( $is_submit )
            {
                $this->load->library('form_validation');
                // Validate 
                $this->form_validation->set_rules('position','Position','trim|required|xss_clean');
                
                // Run validation
                if( $this->form_validation->run() == FALSE )
                {
                    $this->data['title'] = 'Tạo chức vụ mới';                
                    $this->load->view('positions/edit_position_info',$this->data);
                }
                else
                {
                    $position = array(
                        'position'          =>  $this->Position_model->getCurrentData('position'),                             
                    );
                    $this->Position_model->update($position, $id);
                    redirect('admin/positions');
                }
                
            }
            else
            {                                 
                $this->data['title'] = 'Chỉnh sửa thông tin chức vụ ';                
                $this->load->view('positions/edit_position_info',$this->data);
            }
        }

		function deletePositionInfo($id)
        {
            if( !$id )
            {
                redirect('admin/positions');
            }
            
            $this->load->model('Position_model');
            
            if( $this->Position_model->delete($id) )
            {
                redirect('admin/positions');
            }
            else
            {
                redirect('admin/positions');
            }
        }
    }
?>
